<script>
// 东火木
import Vue from 'vue';
export default {
	onLaunch: function() {
		console.log('onLaunch')
		// 仅首次打开一次性加载数据
		this.$store.dispatch('common/init');
		// 同步购物车 本地 & 网络
		this.$store.dispatch('cart/get');
		// 同步 动态数据
		this.$store.dispatch('statistics/get');
		this.get_beizhu()
	},
	onShow: function() {
		console.log('onShow')
		let platform = uni.getSystemInfoSync().platform
		console.log(platform)
		this.$store.dispatch('chat/start');
	},
	 onHide: function() {
		// 断开即时通讯
		this.$store.dispatch('chat/close');
		console.log('App Hide')
		if(this.$store.state.isCAMERA){
			 //#ifdef APP-PLUS
			 let result = this.$store.dispatch("quanxian/requestPermissions",'CAMERA')
				console.log(result) 
			 if (result !== 1) return
			 // #endif
		}
		setTimeout(()=>{
			this.$store.state.isCAMERA = false
		},200)
	},
	methods:{
		getversion() {
			let t = this;
			plus.runtime.getProperty( plus.runtime.appid, (wgtInfo) => {
				const locationversion = wgtInfo.version; 
				t.getnewversion(locationversion)
			})
		},
		getnewversion(locationversion) {
			console.log('jinlaile ')
			let t = this;
			t.$api.get({ 
				url: '/wanlshop/common/update',
				loading: false
			}).then((res) => {
				console.log(res)
				if(res.data){
					let version = res.data.title
					let url ='https://donghuomu.jiuhuanmall.com'+res.data.androidLink
					if(locationversion < version){
						console.log(url)
						let downloadTask = uni.downloadFile({
							url: url, //下载地址
							success: (downloadResult) => {
								console.log('下载')
								uni.showToast({
									title: '下载成功', 
									icon: 'none'
								});
								console.log(downloadResult)
								if (downloadResult.statusCode === 200) {
									uni.hideLoading();
									uni.showLoading({
										title: '正在安装',
									});
									// #ifdef APP-PLUS
									plus.runtime.install(downloadResult.tempFilePath, {
										force: true
									}, function() {
										uni.hideLoading()
										uni.showToast({
											title: '更新完成',
											duration: 1500
										})
										setTimeout(res => {
											plus.runtime.restart();
										}, 1500)
									}, function(e) {
										console.log(e)
										uni.hideLoading()
										uni.showToast({
											title: '更新失败',
											icon: 'none'
										})
									});
									// #endif
								}
							},
							fail(erron) {
								console.log(erron)
							}
						});
						downloadTask.onProgressUpdate((res) => {
							uni.showLoading({
								title: `正在下载 ${res.progress}%`
							});
						});
					}
				}
			});
		},
		get_beizhu(){
			let that = this
			that.$api.get({
				url: '/wanlshop/common/rate',
				success: res => {
					uni.setStorageSync('mulriple',res.data.mulriple)
				}
			});
		},
		//app热更新
		updateApp(newVersionStr, downUrl) {
			// 获取当前版本号
			// #ifdef APP-PLUS
			plus.runtime.getProperty(plus.runtime.appid, widgetInfo => {
				let newVersion = newVersionStr.split('.');
				let oldVersion = widgetInfo.version.split('.');
				console.log(newVersion)
				console.log(oldVersion)
				// 判断是否需要更新
				if (parseInt(newVersion[0]) > parseInt(oldVersion[0]) || parseInt(
						newVersion[1]) > parseInt(oldVersion[1]) ||
					parseInt(newVersion[2]) > parseInt(oldVersion[2])) {
					console.log(newVersion, oldVersion, downUrl);
					uni.showModal({
						title: '提示',
						content: '有新版本，是否更新？',
						success: (res) => {
							if (res.confirm) {
								uni.showLoading({
									title: '正在更新中...',
									mask: true
								})
								uni.downloadFile({
									url: downUrl,
									success: downloadResult => {
										if (downloadResult.statusCode === 200) {
											plus.runtime.install(downloadResult.tempFilePath, {
												force: false
											}, function() {
												uni.hideLoading()
												uni.showToast({
													title: '更新完成',
													duration: 1500
												})
												setTimeout(res => {
													plus.runtime.restart();
												}, 1500)
											}, function(e) {
												uni.hideLoading()
												uni.showToast({
													title: '更新失败',
													icon: 'none'
												})
											});
										}
									},
								})
							}
						}
					})
				}
			})
			// #endif
		},
		tt(title,icon='none',mask=true){
				uni.showToast({
					title,
					icon,
					mask
				})
			}
		}
}
</script>

<style lang="scss">
@import '@/uni_modules/uview-ui/index.scss';
@import url('@/static/css/flex.css');
@import url('@/static/css/myfont.css');

/* #ifndef APP-NVUE */
@import '@/static/style/colorui.css';
@import '@/static/style/wanlshop.css';
@import '@/static/style/iconfont.css';
/* #endif */

page,
view,
image,
text,
input,
button,
scroll-view,
swiper,
swiper-item,
navigator {
	box-sizing: border-box;
	margin: 0;
	padding: 0;
}
.iphonex {
	box-sizing: content-box;
	padding-bottom: env(safe-area-inset-bottom);
	position: fixed;
	bottom: 0;
	left: 0;
	z-index: 99;
	width: 100%;
}

.status_bar {
	height: var(--status-bar-height);
	width: 100%;
}

.plac {
	height: calc(var(--status-bar-height) + 44px);
}

.nav-pt {
	padding-top: calc(var(--status-bar-height) + 44px);
}
@font-face {
	font-family: 'Alibaba';
	// font-family: 'ooo';
	// src: url('~@/static/fonts/webfont.ttf');
	src: url('~@/static/fonts/AlibabaPuHuiTiMedium.ttf');
}
@font-face {
	font-family: 'lll';
	src: url('~@/static/fonts/111.TTF');
}
@font-face {
	font-family: 'xxx';
	src: url('~@/static/fonts/AlibabaPuHuiTiMedium.ttf');
}
uni-rich-text img {
	max-width: 100% !important;
}
</style>
